﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using HTML;
using System.Globalization;
using System.Text;
using System.Web.UI.HtmlControls;
namespace ERP.Page
{
    public partial class Order_Buy : System.Web.UI.Page
    {
        #region[Define]
        protected string StrAlert = string.Empty;
        protected string _StrAlert = string.Empty;
        protected string _StrAlertCus = string.Empty;
        private DataTable dtTotal;
        private DataTable dtOrderBuy;
        private DataTable dtCustomer;
        private DataTable dtCustomerDetail;
        private DataTable dtOrderBuyDetail;
        private DataTable dtOrderDetailTemp;
        private DataTable dtOrder;
        private DataTable dtOrderBuyOrder;
        private Order_BLL clsOrder = new Order_BLL();
        private Order_Detail_BLL clsOrderDetailTemp = new Order_Detail_BLL();
        private Order_Buy_BLL clsOrderBuy = new Order_Buy_BLL();
        private Customer_BLL clsCustomer = new Customer_BLL();
        private Materials_BLL clsMaterials = new Materials_BLL();
        private Order_Detail_BLL clsOrderBuyOrder = new Order_Detail_BLL();
        protected string _TongTienHang = string.Empty;
        protected string _TongTienCK = string.Empty;
        protected string _TongTienVAT = string.Empty;
        protected string _TongTienThanhToan = string.Empty;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetOrder();
                GetMaterials();
                GetOrderBuy();
                GetCustomer();
                GetOrderDetailTemp();
                drSoDonHang.Enabled = false;

                txtMaKhachHang.ReadOnly = true;
                txtTenKhachHang.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Clear();
            rdMuaHangTrucTiep.Checked = true;
            Order_Detail_BLL.DeleteOrderDetailTempAll();
            GetOrderDetailTemp();
            ListViewAllMaterialBuy.Items.Clear();
            ListViewAllMaterialBuy.Visible = false;
            OpenDialog("#orderbuy");
            upDateOrderBuy.Update();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
        protected void btnReload_Click(object sender, EventArgs e)
        {

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                var chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkHide.Checked)
                {
                    try
                    {
                        var dataKey = ListViewAll.DataKeys[i];
                        if (dataKey != null) Order_Buy_BLL.DeleteOrderBuy(int.Parse(dataKey.Value.ToString()));
                    }
                    catch
                    {
                        //((Label)Master.FindControl("lbThongBao")).Text = "Mẫu tin còn tồn tại !";
                        //Master.OpenDialog("#thongbao");
                        //((UpdatePanel)Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            GetOrderBuy();
        }
        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }
        //------------Popup---------
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (hdMaKH.Value.ToString() == string.Empty)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn khách hàng !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (txtChungTu.Text == string.Empty)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng nhập vào chứng từ !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (ListViewOrderDetailTemp.Items.Count <= 0 || ListViewAllMaterialBuy.Items.Count < 0)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn hàng hoá !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (hdMaKH.Value.ToString() != string.Empty && txtChungTu.Text != string.Empty && ListViewOrderDetailTemp.Items.Count > 0 || ListViewAllMaterialBuy.Items.Count > 0)
            {
                InsertOrderBuy();
            }
        }
        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected void btnAddMaterials_Popup_Click(object sender, EventArgs e)
        {
            GetMaterials();
            upDateMaterrials.Update();
            OpenDialog("#materials");
        }
        protected void btnSearchCustomer_Click(object sender, EventArgs e)
        {
            if (txtSearchCustomer.Text == "")
            {
                _StrAlertCus = Alert.AlertWarning("Vui lòng nhập vào từ khoá tìm kiếm !");
            }
            else
            {
                GetCustomer();
            }
        }
        protected void btnReloadCustomer_Click(object sender, EventArgs e)
        {
            txtSearchCustomer.Text = string.Empty;
            GetCustomer();
        }
        protected void btnSearchUC_Click(object sender, EventArgs e)
        {
            if (txtSearchUC.Text == "")
            {
                _StrAlert = Alert.AlertWarning("Vui lòng nhập vào từ khoá tìm kiếm !");
            }
            else
            {
                GetMaterials();
            }
        }
        protected void btnReloadUC_Click(object sender, EventArgs e)
        {
            txtSearchUC.Text = string.Empty;
            GetMaterials();
            GetOrderBuy();
        }
        protected void btnAddMaterials_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAllMaterials.Items.Count; i++)
            {
                var chkHide = (CheckBox)ListViewAllMaterials.Items[i].FindControl("chkListOrderTemp");
                if (chkHide.Checked)
                {
                    try
                    {
                        var dataKey = ListViewAllMaterials.DataKeys[i];
                        if (dataKey != null)
                        {
                            if (CheckInsertM(int.Parse(dataKey.Value.ToString())))
                            {
                                _StrAlert = HTML.Alert.AlertError("Vật tư này đã tồn tại.Vui lòng cập nhật số lượng và đơn giá !");
                            }
                            else
                            {
                                ET.OrderDetailTemp_ET obj = new ET.OrderDetailTemp_ET();
                                obj.Materials_Id = int.Parse(dataKey.Value.ToString());
                                obj.Order_Detail_Quality = 0;

                                obj.Order_Detail_CK_Percent = 0;
                                obj.Order_Detail_CK = 0;

                                obj.Order_Detail_VAT_Percent = 0;
                                obj.Order_Detail_VAT = 0;

                                obj.Order_Detail_Price = 0;
                                Order_Detail_BLL.InsertOrderDetailTemp(obj);
                                GetOrderDetailTemp();
                                upDateOrderBuy.Update();
                                _StrAlert = HTML.Alert.AlertError("Thêm mới vật tư thành công !");
                                CloseDialog("#materials");
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        _StrAlert = HTML.Alert.AlertError("Có lỗi xảy tra trong quá trình thêm mới !" + ex.ToString());
                    }
                }
            }
        }
        //--------------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Acceept")
            {
                Label lbIDOrder = (Label)e.Item.FindControl("lbIDOrder");
                GetOrderBuyDetail(int.Parse(lbIDOrder.Text));
                OpenDialog("#detailmaterials");
                UpdatePanel2.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
        //--------------------------
        protected void ListViewOrderDetailTemp_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                GetOrderDetailTemp();
            }
        }
        protected void ListViewOrderDetailTemp_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            ListViewOrderDetailTemp.EditIndex = -1;
            GetOrderDetailTemp();
        }
        protected void ListViewOrderDetailTemp_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            Label lbID = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("lbID")) as Label;
            Order_Detail_BLL.DeleteOrderDetailTemp(int.Parse(lbID.Text));

            ListViewOrderDetailTemp.EditIndex = -1;
            GetOrderDetailTemp();
            if (Page.Master != null)
                ((Label)Page.Master.FindControl("lbThongBao")).Text = "Xoá vật tư ra khỏi danh sách thành công !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
        }
        protected void ListViewOrderDetailTemp_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListViewOrderDetailTemp.EditIndex = e.NewEditIndex;
            GetOrderDetailTemp();
        }
        protected void ListViewOrderDetailTemp_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            try
            {
                Label lbID = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("lbID")) as Label;

                TextBox txtSoLuong = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtSoLuong")) as TextBox;
                TextBox txtDonGia = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtDonGia")) as TextBox;
                Label txtThanhTien = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtThanhTien")) as Label;

                TextBox txtCKPercent = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtCKPercent")) as TextBox;
                TextBox txtCK = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtCK")) as TextBox;

                TextBox txtVATPercent = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtVATPercent")) as TextBox;
                TextBox txtVAT = (ListViewOrderDetailTemp.Items[e.ItemIndex].FindControl("txtVAT")) as TextBox;

                ET.OrderDetailTemp_ET obj = new ET.OrderDetailTemp_ET();
                obj.Order_Detail_Id = int.Parse(lbID.Text);
                obj.Order_Detail_Quality = int.Parse(txtSoLuong.Text);
                obj.Order_Detail_Price = float.Parse(txtDonGia.Text.Replace(",", ""));

                obj.Order_Detail_CK_Percent = float.Parse(txtCKPercent.Text.Replace(",", ""));
                if (txtCKPercent.Text == "0" || txtCKPercent.Text == string.Empty)
                {
                    obj.Order_Detail_CK = 0;
                }
                else
                {
                    obj.Order_Detail_CK = (float.Parse(txtSoLuong.Text.Replace(",", "")) * (float.Parse(txtDonGia.Text.Replace(",", "")) * float.Parse(txtCKPercent.Text.Replace(",", "")))) / 100;
                }

                obj.Order_Detail_VAT_Percent = float.Parse(txtVATPercent.Text.Replace(",", ""));
                if (txtVATPercent.Text == "0" || txtVATPercent.Text == string.Empty)
                {
                    obj.Order_Detail_VAT = 0;
                }
                else
                {
                    obj.Order_Detail_VAT = (float.Parse(txtSoLuong.Text.Replace(",", "")) * (float.Parse(txtDonGia.Text.Replace(",", "")) * float.Parse(txtVATPercent.Text.Replace(",", "")))) / 100;
                }
                obj.Order_Detail_Total_Price = (float.Parse(txtSoLuong.Text) * float.Parse(txtDonGia.Text.Replace(",", ""))) + float.Parse(txtVAT.Text.Replace(",", "")) - float.Parse(txtCK.Text.Replace(",", ""));

                Order_Detail_BLL.UpdateOrderDetailTemp(obj);

                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Cập nhật thành công danh sách vật tư !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    ListViewOrderDetailTemp.EditIndex = -1;
                    GetOrderDetailTemp();
                    Total();
            }
            catch (Exception ex)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void ListViewOrderDetailTemp_ItemCreated(object sender, ListViewItemEventArgs e)
        {

        }
        //-----------------------
        protected void ListViewAllCustomer_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "_Add")
            {
                Label lbIDCus = (Label)e.Item.FindControl("lbIDCus");
                GetCustomerDetail(int.Parse(lbIDCus.Text));
                upDateOrderBuy.Update();
                CloseDialog("#customer");
            }
        }
        //-----------------------
        protected void rdMuaHangTheoDon_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMuaHangTheoDon.Checked)
            {
                showcustomer.Visible = false;
                drSoDonHang.Enabled = true;
                btnAddMaterials_Popup.Visible = false;
                Clear();
            }
        }
        protected void rdMuaHangTrucTiep_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMuaHangTrucTiep.Checked)
            {
                showcustomer.Visible = true;
                drSoDonHang.Enabled = false;
                drSoDonHang.SelectedIndex = -1;
                btnAddMaterials_Popup.Visible = true;

                hdMaKH.Value = string.Empty;
                txtMaKhachHang.Text = string.Empty;
                txtTenKhachHang.Text = string.Empty;
                txtDiaChi.Text = string.Empty;
                ListViewAllMaterialBuy.Visible = false;
                ListViewAllMaterialBuy.Items.Clear();
            }
        }
        //----------------------
        protected void drSoDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drSoDonHang.SelectedValue.ToString() != "-1")
            {
                GetMaterialOrderDetail(int.Parse(drSoDonHang.SelectedValue.ToString()));
            }
            else
            {
                hdMaKH.Value = string.Empty;
                txtMaKhachHang.Text = string.Empty;
                txtTenKhachHang.Text = string.Empty;
                txtDiaChi.Text = string.Empty;
                ListViewAllMaterialBuy.Visible = false;
                ListViewAllMaterialBuy.Items.Clear();
            }
        }
        #endregion]

        #region[Function]
        protected void GetOrder()
        {
            dtOrder = clsOrder.GetOrderOrder();
            if (dtOrder != null && dtOrder.Rows.Count > 0)
            {
                drSoDonHang.DataSource = dtOrder;
                drSoDonHang.DataTextField = "Order_Code";
                drSoDonHang.DataValueField = "Order_Id";
                drSoDonHang.DataBind();
            }
        }
        protected void GetOrderBuy()
        {
            dtOrderBuy = clsOrderBuy.SearchOrder(txtSearch.Text);
            if (dtOrderBuy != null)
            {
                ListViewAll.DataSource = dtOrderBuy;
                ListViewAll.DataBind();
                if (txtSearch.Text != "" && dtOrderBuy.Rows.Count > 0)
                {
                    StrAlert = Alert.AlertSucess(dtOrderBuy.Rows.Count.ToString(CultureInfo.InvariantCulture));
                }
            }
            else
            {
                StrAlert = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        protected void GetCustomer()
        {
            dtCustomer = clsCustomer.BindGirdtb_Customer(txtSearchCustomer.Text);
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                ListViewAllCustomer.DataSource = dtCustomer;
                ListViewAllCustomer.DataBind();
                if (txtSearchCustomer.Text != "" && dtCustomer.Rows.Count > 0)
                {
                    _StrAlertCus = Alert.AlertSucess(dtCustomer.Rows.Count.ToString(CultureInfo.InvariantCulture));
                }
            }
            else
            {
                _StrAlertCus = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        protected void GetCustomerDetail(int id)
        {
            dtCustomerDetail = clsCustomer.GetCustomerDetail(id);
            if (dtCustomerDetail != null && dtCustomerDetail.Rows.Count > 0)
            {
                hdMaKH.Value = dtCustomerDetail.Rows[0]["Customer_ID"].ToString();
                txtMaKhachHang.Text = dtCustomerDetail.Rows[0]["Customer_Alias"].ToString();
                txtTenKhachHang.Text = dtCustomerDetail.Rows[0]["Customer_Name"].ToString();
                txtDiaChi.Text = dtCustomerDetail.Rows[0]["Customer_Address"].ToString();
            }
        }
        protected void GetMaterialOrderDetail(int id)
        {
            dtOrderBuyOrder = clsOrderBuyOrder.GetOrderDetailOrder(id);
            if (dtOrderBuyOrder != null && dtOrderBuyOrder.Rows.Count > 0)
            {
                ListViewAllMaterialBuy.DataSource = dtOrderBuyOrder;
                ListViewAllMaterialBuy.DataBind();
                ListViewAllMaterialBuy.Visible = true;
                hdMaKH.Value = dtOrderBuyOrder.Rows[0]["Customer_ID"].ToString();
                txtMaKhachHang.Text = dtOrderBuyOrder.Rows[0]["Customer_Alias"].ToString();
                txtTenKhachHang.Text = dtOrderBuyOrder.Rows[0]["Customer_Name"].ToString();
                txtDiaChi.Text = dtOrderBuyOrder.Rows[0]["Customer_Address"].ToString();
            }
            else
            {
                ListViewAllMaterialBuy.Visible = false;
            }
        }
        protected void GetOrderBuyDetail(int id)
        {
            dtOrderBuyDetail = clsOrderBuy.GetOrderBuyDetail(id);
            if (dtOrderBuyDetail != null && dtOrderBuyDetail.Rows.Count > 0)
            {
                ListViewOrderDetail.DataSource = dtOrderBuyDetail;
                ListViewOrderDetail.DataBind();
                ListViewOrderDetail.Visible = true;
                lbOrderCode.Text = dtOrderBuyDetail.Rows[0]["Order_Buy_Code"].ToString();
                lbCustomer_Code.Text = dtOrderBuyDetail.Rows[0]["Customer_Alias"].ToString();
                lbCustomerName.Text = dtOrderBuyDetail.Rows[0]["Customer_Name"].ToString();
                lbDateBegin.Text = Convert.ToDateTime(dtOrderBuyDetail.Rows[0]["Order_Buy_DateBegin"]).ToString("dd/MM/yyyy");
            }
            else
            {
                ListViewOrderDetail.Visible = false;
            }
        }
        protected void GetMaterials()
        {
            DataTable dtMaterials = clsMaterials.Search(txtSearchUC.Text);
            if (dtMaterials != null)
            {
                ListViewAllMaterials.DataSource = dtMaterials;
                ListViewAllMaterials.DataBind();
                if (txtSearchUC.Text != "" && dtMaterials.Rows.Count > 0)
                {
                    _StrAlert = Alert.AlertSucess(dtMaterials.Rows.Count.ToString(CultureInfo.InvariantCulture));
                }
            }
            else
            {
                _StrAlert = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        protected void GetOrderDetailTemp()
        {
            dtOrderDetailTemp = clsOrderDetailTemp.GetOrderDetailTemp();
            if (dtOrderDetailTemp != null)
            {
                ListViewOrderDetailTemp.DataSource = dtOrderDetailTemp;
                ListViewOrderDetailTemp.DataBind();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            DataTable dtOrderBuy = clsOrderBuy.GetOrderBuy();
            if (dtOrderBuy != null && dtOrderBuy.Rows.Count > 0)
            {
                foreach (DataRow dr in dtOrderBuy.Rows)
                {
                    if (txtChungTu.Text == dr["Order_Buy_Code"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertM(int id)
        {
            bool kiemtra = false;
            DataTable dtMaterialsCheck = clsOrderDetailTemp.GetOrderDetailTemp();
            if (dtMaterialsCheck != null && dtMaterialsCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in dtMaterialsCheck.Rows)
                {
                    if (id == int.Parse(dr["Materials_Id"].ToString()))
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertOrderBuy()
        {
            try
            {
                if (CheckInsert())
                {
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Mã chứng từ đã tồn tại !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                }
                else
                {
                    ET.Order_Buy_ET obj = new ET.Order_Buy_ET();
                    ET.ET_Order_Buy_Detail _obj = new ET.ET_Order_Buy_Detail();
                    obj.Order_Id = int.Parse(drSoDonHang.SelectedValue.ToString());
                    obj.Order_Buy_Code = txtChungTu.Text;

                    obj.Order_Buy_DateBegin = DateTime.Parse(txtNgayDatHang.Text);
                    obj.Order_Buy_DateEnd = DateTime.Parse(txtHangThanhToan.Text);

                    obj.Order_Buy_Tax_Code = txtMaSoThue.Text;
                    obj.Order_Buy_Description = txtDienGiai.Text;
                    obj.Order_Buy_Payment_Type = txtHinhThucThanhToan.Text;
                    obj.Order_Buy_Uesr_ID = 0;
                    obj.Customer_ID = int.Parse(hdMaKH.Value.ToString());
                    Order_Buy_BLL.InsertOrderBy(obj);
                    #region
                    if (rdMuaHangTrucTiep.Checked)
                    {
                        for (int i = 0; i < ListViewOrderDetailTemp.Items.Count; i++)
                        {
                            HtmlInputHidden SenderId = (HtmlInputHidden)ListViewOrderDetailTemp.Items[i].FindControl("hiddenId");

                            Label lbID = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lbID");
                            Label lbIDMaterials_Id = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lbIDMaterials_Id");
                            Label lbSoLuong = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lbSoLuong");
                            Label lbDonGia = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lbDonGia");

                            Label lb_lst_ck_percent = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lb_lst_ck_percent");
                            Label lb_lst_ck = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lb_lst_ck");

                            Label lb_lst_vat_pertcent = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lb_lst_vat_pertcent");
                            Label lb_lst_vat = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lb_lst_vat");

                            Label lbThanhTien = (Label)ListViewOrderDetailTemp.Items[i].FindControl("lbThanhTien");

                            _obj.Order_Buy_Id = int.Parse(obj.Order_Buy_Id.ToString());
                            _obj.Materials_Id = int.Parse(lbIDMaterials_Id.Text);
                            _obj.Order_Detail_Quality = int.Parse(lbSoLuong.Text);
                            _obj.Order_Detail_Price = float.Parse(lbDonGia.Text.Replace(",", ""));

                            _obj.Order_Detail_CK_Percent = int.Parse(lb_lst_ck_percent.Text);
                            _obj.Order_Detail_CK = int.Parse(lb_lst_ck.Text);

                            _obj.Order_Detail_VAT_Percent = int.Parse(lb_lst_vat_pertcent.Text);
                            _obj.Order_Detail_VAT = int.Parse(lb_lst_vat.Text);

                            _obj.Order_Detail_Total_Price = float.Parse(lbThanhTien.Text);
                            _obj.IsActive = false;
                            Order_Buy_Detail_BLL.InsertOrderDetail(_obj);
                        }
                        Order_Detail_BLL.DeleteOrderDetailTempAll();
                        GetOrderDetailTemp();
                        GetOrderBuy();
                        upDateOrderBuy.Update();
                        Clear();
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công phiếu đặt hàng !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    #endregion
                    #region
                    if (rdMuaHangTheoDon.Checked)
                    {
                        for (int i = 0; i < ListViewAllMaterialBuy.Items.Count; i++)
                        {
                            HtmlInputHidden SenderId = (HtmlInputHidden)ListViewAllMaterialBuy.Items[i].FindControl("hiddenId");

                            Label lbID = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lbID");
                            Label lbIDMaterials_Id = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lbIDMaterials_Id");
                            Label lbSoLuong = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lbSoLuong");
                            Label lbDonGia = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lbDonGia");

                            Label lb_lst_ck_percent = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lb_lst_ck_percent");
                            Label lb_lst_ck = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lb_lst_ck");

                            Label lb_lst_vat_pertcent = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lb_lst_vat_pertcent");
                            Label lb_lst_vat = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lb_lst_vat");

                            Label lbThanhTien = (Label)ListViewAllMaterialBuy.Items[i].FindControl("lbThanhTien");

                            _obj.Order_Buy_Id = int.Parse(obj.Order_Buy_Id.ToString());
                            _obj.Materials_Id = int.Parse(lbIDMaterials_Id.Text);
                            _obj.Order_Detail_Quality = int.Parse(lbSoLuong.Text);
                            _obj.Order_Detail_Price = float.Parse(lbDonGia.Text.Replace(",", ""));

                            _obj.Order_Detail_CK_Percent = int.Parse(lb_lst_ck_percent.Text);
                            _obj.Order_Detail_CK = int.Parse(lb_lst_ck.Text);

                            _obj.Order_Detail_VAT_Percent = int.Parse(lb_lst_vat_pertcent.Text);
                            _obj.Order_Detail_VAT = int.Parse(lb_lst_vat.Text);

                            _obj.Order_Detail_Total_Price = float.Parse(lbThanhTien.Text);
                            _obj.IsActive = false;
                            Order_Buy_Detail_BLL.InsertOrderDetail(_obj);
                        }
                    #endregion
                        Order_Buy_Detail_BLL.DeleteOrderDetail(int.Parse(drSoDonHang.SelectedValue.ToString()));
                        GetOrder();
                        GetOrderBuy();
                        upDateOrderBuy.Update();
                        Clear();
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công phiếu đặt hàng !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void Total()
        {
            dtTotal = clsOrderBuyOrder.GetOrderDetailTempTotal();
            if (dtTotal != null && dtTotal.Rows.Count > 0)
            {
                _TongTienHang = String.Format("{0:0.00}", dtTotal.Rows[0]["TONG TIEN HANG"].ToString());
                _TongTienCK = dtTotal.Rows[0]["TONG TIEN CK"].ToString();
                _TongTienVAT = dtTotal.Rows[0]["TONG TIEN VAT"].ToString();
                _TongTienThanhToan = String.Format("{0:0.00}", dtTotal.Rows[0]["TONG TIEN THANH TOAN"].ToString());
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[ClearText]
        protected void Clear()
        {
            txtMaKhachHang.Text = string.Empty;
            txtTenKhachHang.Text = string.Empty;
            txtMaSoThue.Text = string.Empty;
            txtNgayDatHang.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtDiaChi.Text = string.Empty;
            txtDienGiai.Text = string.Empty;
            txtHangThanhToan.Text = string.Empty;

            dtOrderBuy = clsOrderBuy.GetOrderBuy();
            if (dtOrderBuy != null && dtOrderBuy.Rows.Count > 0)
            {
                txtChungTu.Text = "PN" + "." + DateTime.Now.ToString("MMdd") + "-" + "000" + dtOrderBuy.Rows.Count.ToString();
            }
            else
            {
                txtChungTu.Text = "PN" + "." + DateTime.Now.ToString("MMdd") + "-" + "0000";
            }
            txtNgayDatHang.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtHangThanhToan.Text = DateTime.Today.ToString("dd/MM/yyyy");
            drNguoiLapPhieu.SelectedIndex = -1;
            drSoDonHang.SelectedIndex = -1;
        }
        #endregion
    }
}